<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试名称">
        <a-input placeholder="考试名称" v-decorator="['title', {rules: [{required: true, message: '请输入考试名称'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试描述">
        <a-input placeholder="考试描述" v-decorator="['content', {rules: [{required: true, message: '请输入考试描述'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="1公开2部门3定员">
        <a-select placeholder="请选择1公开2部门3定员" v-decorator="['openType', {rules: [{required: true, message: '请选择1公开2部门3定员'}]}]" allowClear>
          <a-select-option v-for="(b, index) in openTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="组题方式1题库,2指定">
        <a-select placeholder="请选择组题方式1题库,2指定" v-decorator="['joinType', {rules: [{required: true, message: '请选择组题方式1题库,2指定'}]}]" allowClear>
          <a-select-option v-for="(b, index) in joinTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="难度:0不限,1普通,2较难">
        <a-input placeholder="难度:0不限,1普通,2较难" v-decorator="['level', {rules: [{required: true, message: '请输入难度:0不限,1普通,2较难'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试状态">
        <a-input placeholder="考试状态" v-decorator="['state', {rules: [{required: true, message: '请输入考试状态'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否限时">
        <a-input placeholder="是否限时" v-decorator="['timeLimit', {rules: [{required: true, message: '请输入是否限时'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总分数">
        <a-input placeholder="总分数" v-decorator="['totalScore', {rules: [{required: true, message: '请输入总分数'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总时长">
        <a-input placeholder="总时长" v-decorator="['totalTime', {rules: [{required: true, message: '请输入总时长'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="及格分数">
        <a-input placeholder="及格分数" v-decorator="['qualifyScore', {rules: [{required: true, message: '请输入及格分数'}]}]" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveElExam } from '@/api/elExam'
import pick from 'lodash.pick'

export default {
  name: 'ElExamModal',
  props: {
    openTypeList: {
      type: Array,
      required: true
    },
    joinTypeList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate() {
  },
  created() {
  },
  methods: {
    add() {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    ruleAdd () {
      this.visible = true
      // this.form.resetFields()
      // this.edit({ id: 0 })
    },
    edit(record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'title', 'content', 'openType', 'joinType', 'level', 'state', 'timeLimit', 'startTime', 'endTime', 'createTime', 'updateTime', 'totalScore', 'totalTime', 'qualifyScore'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveElExam(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
